package com.foxit.pdf2pdf.web.common;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import com.google.code.morphia.Datastore;


public class GetBean {
	/**
	 * initialize flag.
	 */
	private static boolean inited = false;
	private static Datastore datastore = null;
	private static final Logger log = Logger.getLogger(GetBean.class);
	
	/**
	 * @param configXmlResource bean configuration
	 * @return return true if initialize successfully, otherwise return false.
	 */
	public static boolean InitContext(Resource configXmlResource)
   {
       if (!inited)
       {
    	//log.debug("getBean");
       	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
       	//log.debug("beanFactory:"+beanFactory);
       	BeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
       	//log.debug("reader:"+reader);
       	reader.loadBeanDefinitions(configXmlResource);
       	//log.debug("reader.loadBeanDefinitions(configXmlResource) end ");
       	datastore = (Datastore)beanFactory.getBean("datastore");
       	//beanFactory.getBean("urlBean");
       	
       	//log.debug("reader bean end");
       }
       inited = true;
       return true;
   }

	public static Datastore getDatastore() {
		return datastore;
	}



	public static void setDatastore(Datastore datastore) {
		GetBean.datastore = datastore;
	}
}
